package comic.content.db;

import java.io.ByteArrayOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;

public class ComicDbAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String COMICID = "comicid";
	public static final String NAME = "name";
	public static final String IMAGE = "image";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private static final String DATABASE_CREATE = "create table t_comic (_id integer primary key autoincrement, "
			+ COMICID + " text, " + NAME + " text ," + IMAGE + " blob );";
	private static final String DATABASE_NAME = "comicBitmapdatabase";
	private static final String DATABASE_TABLE = "t_comic";
	private static final int DATABASE_VERSION = 1;
	private final Context mCtx;

	public ComicDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public ComicDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void closeclose() {
		mDbHelper.close();
	}

	public boolean deleteDiary(String comicid) {
		return mDb.delete(DATABASE_TABLE, COMICID + "=" + comicid, null) > 0;
	}

	public long insertContent(String comicid, String name, Bitmap icon) {
		if (icon != null) {
			try {
				ContentValues initialValues = new ContentValues();
				initialValues.put(COMICID, comicid);
				initialValues.put(NAME, name);
				ByteArrayOutputStream os = new ByteArrayOutputStream();
				// 将Bitmap压缩成PNG编码，质量为100%存储
				icon.compress(Bitmap.CompressFormat.PNG, 100, os);
				// 写入数据库的Browser.BookmarkColumns.TOUCH_ICON字段
				initialValues.put(IMAGE, os.toByteArray());
				deleteDiary(comicid);// 删除重复数据
				return mDb.insert(DATABASE_TABLE, null, initialValues);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	public Cursor getAllNotes() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, COMICID,
				NAME }, null, null, null, null, KEY_ROWID + " desc");
	}

	public Cursor getContent(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, COMICID, NAME, IMAGE }, KEY_ROWID + "=" + rowId,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public Cursor getNextContent(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				COMICID, NAME, IMAGE }, KEY_ROWID + "<" + rowId, null, null,
				null, KEY_ROWID + " desc ");
		return mCursor;

	}

	public Cursor getPreContent(long rowId) throws SQLException {
		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				COMICID, NAME, IMAGE }, KEY_ROWID + ">" + rowId, null, null,
				null, KEY_ROWID + " asc");
		return mCursor;

	}

	public static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
			onCreate(db);

		}

	}
}
